package com.example.demo.controller;

import com.example.demo.util.Dog;
import com.example.demo.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.*;

@Slf4j
@RestController
public class TestController {

    @RequestMapping("/test")
    public String testA(){
        log.info("ccccccccccccc");
        return "ha ha  ...................";
    }

    public String str;

    @RequestMapping("/a")
    public String test(@RequestParam(name = "name") String name,
                      @RequestBody Dog dog){
        log.info("name ... {}", name);
        log.info("dog ... {}", JsonUtil.toJson(dog));
        str = readJsonFile("D://aa.json");
        return str;
    }


    @GetMapping("/import")
    public String importDer(){
        str = readJsonFile("D://aa.json");
        return str;
    }


    @GetMapping("/get")
    public String get(){
        str = readJsonFile("D://aa.json");
        return str;
    }

    public String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
